<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\VehicleSeries;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Admin\Renderable\DealerTable;
use App\Models\Dealer;

class VehicleSeriesController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new VehicleSeries(), function (Grid $grid) {
            $grid->model()->orderBy('order');
            $grid->column('id')->sortable();
            $grid->column('series_name');
            $grid->column('order')->orderable();
            $grid->column('recommend', '是否开启购车送积分')->switch();
            $grid->column('clue_point')->editable(true);
            $grid->column('driver_point')->editable(true);
            $grid->column('buy_car_point')->editable(true);
            $grid->column('created_at')->sortable();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id')->integer()->width(2);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new VehicleSeries(), function (Show $show) {
            $show->field('id');
            $show->field('series_name');
            $show->field('order');
            $show->field('recommend')->using([0 => '否', 1 => '是']);
            $show->column('clue_point');
            $show->column('driver_point');
            $show->column('buy_car_point');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $model = VehicleSeries::with('dealers');
        return Form::make($model, function (Form $form) {
            $form->display('id');
            $form->text('series_name')->required();
            // $form->number('order')->required();
            $form->multipleSelectTable('dealers', '经销商')
                ->from(DealerTable::make(['id' => $form->getKey()])) // 设置渲染类实例，并传递自定义参数
                ->options(function ($v) { // 设置编辑数据显示
                    if (!$v) {
                        return [];
                    }
                    return Dealer::find($v)->pluck('name', 'id');
                })
                ->customFormat(function ($v) {
                    if (!$v) {
                        return [];
                    }
                    // 从数据库中查出的二维数组中转化成ID
                    return array_column($v, 'id');
                })->required();

            $form->image('poster')->disk('obs')->removable(false)->uniqueName()->saveFullUrl()->autoUpload()->required();
            $form->switch('recommend');
            $form->number('clue_point');
            $form->number('driver_point');
            $form->number('buy_car_point');
            $form->image('recommend_poster')->disk('obs')->removable(false)->uniqueName()->saveFullUrl()->autoUpload()->required();
        });
    }
}
